package com.zzp.basepresenter.listener;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.RequiresApi;

import com.zzp.basepresenter.util.LoggerUtils;

import org.json.JSONException;
import org.xutils.common.Callback;

import java.util.concurrent.ThreadLocalRandom;

public class xUtilCallBack implements Callback.CommonCallback<String> {


    private int success = 0x196;
    private int failed = 0x197;
    private int finish = 0x198;
    private String url;

    public xUtilCallBack() {
    }

    public xUtilCallBack(String url) {
        this.url = url;
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == success) {
                if (msg.obj instanceof String) {
                    try {
                        onJson((String) msg.obj);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            } else if (msg.what == failed) {
                onFailed();
            } else if (msg.what == finish) {
                onComplete((Boolean) msg.obj);
            }
        }
    };

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onSuccess(String s) {
        if (s == null || s.equals("")) {
            LoggerUtils.getInstance().i("return json is null");
            return;
        }
        if (url != null) {
            LoggerUtils.getInstance().i("background  " + url);
        }
        success = random(0, 9999);
        Message message = new Message();
        message.what = success;
        message.obj = s;
        handler.sendMessage(message);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onError(Throwable throwable, boolean b) {
        failed = random(0, 9999);
        Message message = new Message();
        message.what = failed;
        handler.sendMessage(message);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onCancelled(CancelledException e) {
        finish = random(0, 9999);
        Message message = new Message();
        message.what = finish;
        message.obj = false;
        handler.sendMessageDelayed(message, 1000);
    }

    @Override
    public void onFinished() {

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void onJson(String json) throws JSONException {
        LoggerUtils.getInstance().i("onJson  \t\t" + json);
        finish = random(0, 9999);
        Message message = new Message();
        message.what = finish;
        message.obj = true;
        handler.sendMessageDelayed(message, 1000);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void onFailed() {
        finish = random(0, 9999);
        Message message = new Message();
        message.what = finish;
        message.obj = false;
        handler.sendMessageDelayed(message, 1000);
    }

    public void onComplete(boolean suc) {
        LoggerUtils.getInstance().i("finished all socket " + suc);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private int random(int MIX_RANGE, int MAX_RANGE) {
        return ThreadLocalRandom.current().nextInt(MIX_RANGE, MAX_RANGE);
    }
}
